﻿Imports ajayumi.develop.win32.Transport.Contracts
Imports System.Windows.Forms
Imports System.Threading
Imports System.IO
Imports System.ServiceModel
Imports ajayumi.develop.win32.Transport.Wcf.Contracts
Imports ajListView = ajayumi.develop.VBasic.Win.Controls.ListView

<ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Multiple)> _
Public Class DownloadFileService
    Implements IDownloadFile

#Region "variable"

    Private m_Callback As ICallback = Nothing

    Public Shared Txt As TextBox = Nothing
    Public Shared SyncContext As SynchronizationContext
    Public Shared Lv As ajListView = Nothing

#End Region

#Region "property"


#End Region

#Region "private method"

    ''' <summary>
    ''' 添加项
    ''' </summary>
    ''' <param name="state"></param>
    ''' <remarks></remarks>
    Private Sub addItem(ByVal state As Object)
        Dim _TransFileInfo As TransFileInfo = DirectCast(state, TransFileInfo)
        Dim _ProgressPercentage As Integer = CInt(CDbl(_TransFileInfo.Position) / CDbl(_TransFileInfo.FileLength) * 100.0#)
        Dim _ColCount As Integer = Lv.Columns.Count - 1
        Dim _ListViewSubItems(_ColCount) As ListViewItem.ListViewSubItem

        _ListViewSubItems(0) = New ListViewItem.ListViewSubItem()
        With _ListViewSubItems(0)
            .Name = Lv.Columns(0).Name
            .Text = _TransFileInfo.Guid
        End With

        _ListViewSubItems(1) = New ListViewItem.ListViewSubItem()
        With _ListViewSubItems(1)
            .Name = Lv.Columns(1).Name
            .Text = _TransFileInfo.FilePath
        End With

        _ListViewSubItems(2) = New ListViewItem.ListViewSubItem()
        With _ListViewSubItems(2)
            .Name = Lv.Columns(2).Name
            .Text = _TransFileInfo.SavePath
        End With

        _ListViewSubItems(3) = New ListViewItem.ListViewSubItem()
        With _ListViewSubItems(3)
            .Name = Lv.Columns(3).Name
            .Text = _TransFileInfo.FileLength
        End With

        _ListViewSubItems(4) = New ListViewItem.ListViewSubItem()
        With _ListViewSubItems(4)
            .Name = Lv.Columns(4).Name
            .Text = CStr(_ProgressPercentage)
        End With

        _ListViewSubItems(5) = New ListViewItem.ListViewSubItem()
        With _ListViewSubItems(5)
            .Name = Lv.Columns(5).Name
            .Text = TransportStatus.Transporting.ToString()
        End With

        Dim _ListViewItem As New ListViewItem(_ListViewSubItems, -1)
        Lv.Items.Add(_ListViewItem)
    End Sub

    ''' <summary>
    ''' 更新项
    ''' </summary>
    ''' <param name="state"></param>
    ''' <remarks></remarks>
    Private Sub uploadItem(ByVal state As Object)
        Dim _TransFileInfo As TransFileInfo = DirectCast(state, TransFileInfo)
        Dim _ProgressPercentage As Integer = CInt(CDbl(_TransFileInfo.Position) / CDbl(_TransFileInfo.FileLength) * 100.0#)
        Dim _ListViewItem As ListViewItem = Nothing

        For Each _ListViewItem In Lv.Items
            If _ListViewItem.SubItems(Settings.GUID_KEY).Text.Equals(_TransFileInfo.Guid) Then
                Exit For
            End If
        Next

        _ListViewItem.SubItems(Settings.PROGRESS_KEY).Text = CStr(_ProgressPercentage)

        If _ProgressPercentage >= 100 Then
            _ListViewItem.SubItems(Settings.STAUTS_KEY).Text = TransportStatus.Transported.ToString()
        Else
            _ListViewItem.SubItems(Settings.STAUTS_KEY).Text = TransportStatus.Transporting.ToString()
        End If

    End Sub



    Private Sub doPost(ByVal state As Object)
        Txt.AppendText(state.ToString())
    End Sub

#End Region

#Region "public method"

    Public Sub TransportFile(ByVal transFileInfo As Contracts.TransFileInfo) Implements Contracts.ITransportFile.TransportFile
        Me.m_Callback = OperationContext.Current.GetCallbackChannel(Of ICallback)()
        Dim _TempStr As String = String.Empty

        If transFileInfo.Position = 0L Then
            SyncContext.Post(New SendOrPostCallback(AddressOf Me.addItem), transFileInfo)
        End If

        Using _FileStream As New IO.FileStream(transFileInfo.SavePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
            _FileStream.Seek(transFileInfo.Position, SeekOrigin.Begin)
            _FileStream.Write(transFileInfo.FileData, 0, transFileInfo.FileData.Length)

            _TempStr = String.Format("服务端收到来自客户端发送的 {0} 文件的数据量：{1}字节{2}", _
                                     Path.GetFileName(transFileInfo.FilePath), _
                                     transFileInfo.FileData.Length, vbCrLf)
            SyncContext.Post(New SendOrPostCallback(AddressOf Me.doPost), _TempStr)

            transFileInfo.Position += transFileInfo.FileData.Length
            Me.m_Callback.OnProgressChanged(transFileInfo)

            If transFileInfo.Position >= transFileInfo.FileLength Then
                Me.m_Callback.OnCompleted(transFileInfo)
                _TempStr = String.Format("服务端接收文件 {0} 完毕{1}", _
                                         Path.GetFileNameWithoutExtension(transFileInfo.FilePath), vbCrLf)
                SyncContext.Post(New System.Threading.SendOrPostCallback(AddressOf Me.doPost), _TempStr)
            End If

            SyncContext.Post(New SendOrPostCallback(AddressOf Me.uploadItem), transFileInfo)
        End Using

    End Sub

#End Region

End Class
